package tutorial.java.dragdrop;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import javax.swing.border.EtchedBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class CheckboxDemo extends JFrame {

	private JPanel contentPane;
	private JCheckBox cbRed;
	private JCheckBox cbBlue;
	private JCheckBox cbYellow;
	private JCheckBox cbSelectAll;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CheckboxDemo frame = new CheckboxDemo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CheckboxDemo() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		cbRed = new JCheckBox("Red");
		cbRed.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					System.out.println("Red is selected");
				}
			}
		});
		cbRed.setBounds(26, 27, 70, 23);
		contentPane.add(cbRed);
		
		cbBlue = new JCheckBox("Blue");
		cbBlue.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					System.out.println("Blue is selected");
				}
			}
		});
		cbBlue.setBounds(26, 74, 70, 23);
		contentPane.add(cbBlue);
		
		cbYellow = new JCheckBox("Yellow");
		cbYellow.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					System.out.println("Yellow is selected");
				}
			}
		});
		cbYellow.setBounds(26, 125, 70, 23);
		contentPane.add(cbYellow);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBounds(10, 21, 106, 140);
		contentPane.add(panel);
		
		JButton btnCheck = new JButton("Check");
		btnCheck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sum = "";
				if (cbRed.isSelected()) {
					sum += " Red";
				} 
				if (cbBlue.isSelected()) {
					sum += " Blue";
				}
				if (cbYellow.isSelected()) {
					sum += " Yellow";
				}
				if (!sum.equals("")) {
					JOptionPane.showMessageDialog(null, sum + " is selected");
				} else {
					JOptionPane.showMessageDialog(null, "Please choose at least 1 box");
				}
			}
		});
		btnCheck.setBounds(26, 210, 70, 23);
		contentPane.add(btnCheck);
		
		cbSelectAll = new JCheckBox("Select All");
		cbSelectAll.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				boolean checkAll = cbSelectAll.isSelected();
				if (checkAll) {
					cbRed.setSelected(true);
					cbBlue.setSelected(true);
					cbYellow.setSelected(true);
				} else {
					cbRed.setSelected(false);
					cbBlue.setSelected(false);
					cbYellow.setSelected(false);
				}
			}
		});
		cbSelectAll.setBounds(26, 168, 90, 23);
		contentPane.add(cbSelectAll);
	}
}
